
var noReportSelected = "Choose a report";

var AssetsExportedByDateCols = new Array();
AssetsExportedByDateCols[0] = "DATEOBJ";
AssetsExportedByDateCols[1] = "LoginName";
AssetsExportedByDateCols[2] = "AssetName";

var AssetsExportedByDateColsDisplay = new Array();
AssetsExportedByDateColsDisplay[0] = "1Date/Time";
AssetsExportedByDateColsDisplay[1] = "1Username";
AssetsExportedByDateColsDisplay[2] = "1Asset Name";

var AssetsExportedByDateAndPersonDepictedCols = new Array();
AssetsExportedByDateAndPersonDepictedCols[0] = "DATEOBJ";
AssetsExportedByDateAndPersonDepictedCols[1] = "LoginName";
AssetsExportedByDateAndPersonDepictedCols[2] = "AssetName";

var AssetsExportedByDateAndPersonDepictedColsDisplay = new Array();
AssetsExportedByDateAndPersonDepictedColsDisplay[0] = "2Date/Time";
AssetsExportedByDateAndPersonDepictedColsDisplay[1] = "2Username";
AssetsExportedByDateAndPersonDepictedColsDisplay[2] = "2Asset Name";

var AssetsExportedByDateAndFilmTitleCols = new Array();
AssetsExportedByDateAndFilmTitleCols[0] = "DATEOBJ";
AssetsExportedByDateAndFilmTitleCols[1] = "LoginName";
AssetsExportedByDateAndFilmTitleCols[2] = "AssetName";

var AssetsExportedByDateAndFilmTitleColsDisplay = new Array();
AssetsExportedByDateAndFilmTitleColsDisplay[0] = "3Date/Time";
AssetsExportedByDateAndFilmTitleColsDisplay[1] = "3Username";
AssetsExportedByDateAndFilmTitleColsDisplay[2] = "3Asset Name";

/*
function setSortSelectOptions(report){
	jQuery('#sortOrderId select').empty();
	for (i=0;i<eval(report + "Cols.length");i++)
	{
		jQuery('<option value="' + eval(report + 'Cols[' + i +']') + '"></option>').html(eval(report + 'ColsDisplay[' + i +']')).appendTo('#sortOrderId select');
	}
}
*/

jQuery(document).ready(
	function(){
		jQuery("#filmTitleInputId").keyup(function(event){

			if ((event.keyCode >= 48 && event.keyCode <= 90) || event.keyCode == 8 || event.keyCode == 32 || event.keyCode == 188) {
		
				jQuery("#filmTitleInputId").show("fast");
		
				var letters = jQuery(this).val();
		
				var paramKey = 'ft';
				var paramVal = letters;
		
				var re =  /^[a-zA-Z]+$/;
		
		//		if (/*letters.length > 1 &&*/ re.test(letters))
				if (letters.length > 0)
				{
					jQuery('#ftAjaxList ul').empty();
		
					jQuery("#ftAjaxList").addClass("ajaxLoading");
					
					jQuery.ajax({
						url: '/CustomReports/AjaxPicklistProxy',
						type: 'GET',
						data: 'paramKey=' + paramKey + '&paramVal=' + paramVal ,
						dataType: 'xml',
						timeout: 30000,
						error: function(){
//							alert('Error loading XML document');
			    		},
						success: function(xml){
							jQuery("#ftAjaxList").removeClass("ajaxLoading");
							var itemName, itemVal = "";
							jQuery('#ftAjaxList ul').empty();
							jQuery(xml).find("item").each(function(){
								itemName = jQuery("name", this).text();
		//						itemVal = jQuery("value", this).text().split("%%")[0];
								jQuery('<li class="ftLi"></li>').html(itemName).appendTo('#ftAjaxList ul');
							});
							attachClickToFtLi();
						}
					});
				}
		}
   	});
}); 

jQuery(document).ready(
	function(){
		jQuery("#personDepictedInputId").keyup(function(event){

			if ((event.keyCode >= 48 && event.keyCode <= 90) || event.keyCode == 8 || event.keyCode == 32 || event.keyCode == 188) {
				jQuery("#personDepictedInputId").show("fast");
		
				var letters = jQuery(this).val();
		
		//		var param = 'pd=' + letters;
		
				var paramKey = 'pd';
				var paramVal = letters;
		
				var re =  /^[a-zA-Z]+$/;
			
		//		if (/*letters.length > 1 &&*/ re.test(letters))
				if (letters.length > 0)
				{
					jQuery('#pdAjaxList ul').empty();
		
					jQuery("#pdAjaxList").addClass("ajaxLoading");
					
					jQuery.ajax({
						url: '/CustomReports/AjaxPicklistProxy',
						type: 'GET',
		//				data: 'Param=' + param,
						data: 'paramKey=' + paramKey + '&paramVal=' + paramVal ,
						dataType: 'xml',
						timeout: 30000,
						error: function(){
//							alert('Error loading XML document');
			    		},
						success: function(xml){
							jQuery("#pdAjaxList").removeClass("ajaxLoading");
							var itemName = "";
							jQuery('#pdAjaxList ul').empty();
							jQuery(xml).find("item").each(function(){
								itemName = jQuery("name", this).text();
								jQuery('<li class="pdLi"></li>').html(itemName).appendTo('#pdAjaxList ul');
							});
							attachClickToPdLi();
						}
					});
				}
		}
   	});
}); 


jQuery(document).ready(
	function(){
		jQuery("#reportNameSelectId").change(
			function () {
				jQuery("select option:selected").each(
					function () {
						var selectedText = jQuery(this).text();
						if (selectedText == noReportSelected){
							jQuery("#datesGroupId").hide('slow');
							jQuery("#submitId").hide('slow');
							jQuery("#numberOfTimesGroupId").hide('slow');
							jQuery("#filmTitleGroupId").hide('slow');
							jQuery("#personDepictedGroupId").hide('slow');							
//							jQuery("#sortOrderGroupId").hide('slow');
						}
						else if (selectedText.indexOf("Film Title") > -1){
//							setSortSelectOptions('AssetsExportedByDateAndFilmTitle');

							jQuery("#submitId").show('slow');
//							jQuery("#sortOrderGroupId").show('slow');
							jQuery("#datesGroupId").show('slow');
							jQuery("#filmTitleGroupId").show('slow');

							jQuery("#numberOfTimesGroupId").hide('slow');
							jQuery("#personDepictedGroupId").hide('slow');
						}
						else if (selectedText.indexOf("Person Depicted") > -1){
//							setSortSelectOptions('AssetsExportedByDateAndPersonDepicted');

//							jQuery("#sortOrderGroupId").show('slow');
							jQuery("#submitId").show('slow');
							jQuery("#datesGroupId").show('slow');
							jQuery("#personDepictedGroupId").show('slow');

							jQuery("#numberOfTimesGroupId").hide('slow');
							jQuery("#filmTitleGroupId").hide('slow');
						}
						else if (selectedText.indexOf("Sorted By User") > -1){
							jQuery("#submitId").show('slow');
							jQuery("#datesGroupId").show('slow');

							jQuery("#numberOfTimesGroupId").hide('slow');
							jQuery("#filmTitleGroupId").hide('slow');
							jQuery("#personDepictedGroupId").hide('slow');
						}
						else if (selectedText.indexOf("By Date") > -1){
//							setSortSelectOptions('AssetsExportedByDate');

//							jQuery("#sortOrderGroupId").show('slow');
							jQuery("#submitId").show('slow');
							jQuery("#datesGroupId").show('slow');

							jQuery("#numberOfTimesGroupId").hide('slow');
							jQuery("#filmTitleGroupId").hide('slow');
							jQuery("#personDepictedGroupId").hide('slow');						
						}
						else if (selectedText.indexOf("Number of Times") > -1){

							jQuery("#submitId").show('slow');
							jQuery("#datesGroupId").show('slow');
							jQuery("#numberOfTimesGroupId").show('slow');

							jQuery("#filmTitleGroupId").hide('slow');
							jQuery("#personDepictedGroupId").hide('slow');						
						}
					}
				);
			}
		);
	}
);

jQuery(document).ready(
	function(){
		jQuery("#numberOfTimesInputId").change(
			function () {
				jQuery("select option:selected").each(
					function () {
						var selectedText = jQuery(this).text();
						if (selectedText == "Make Selection"){

							clearNTFields();
							
							jQuery("#numberOfTimesValueInputId").hide('slow');
							jQuery("#numberOfTimesBetweenAndId").hide('slow');
							jQuery("#numberOfTimesBetweenEndId").hide('slow');
						}
						else if (selectedText.indexOf("At Least") > -1){

							clearNTFields();
							
							jQuery("#numberOfTimesValueInputId").show('slow');
							jQuery("#numberOfTimesBetweenAndId").hide('slow');
							jQuery("#numberOfTimesBetweenEndId").hide('slow');
						}
						else if (selectedText.indexOf("At Most") > -1){

							clearNTFields();
							
							jQuery("#numberOfTimesValueInputId").hide('slow');
							jQuery("#numberOfTimesBetweenAndId").hide('slow');
							jQuery("#numberOfTimesBetweenEndId").show('slow');
						}
						else if (selectedText.indexOf("Exactly") > -1){

							clearNTFields();
							
							jQuery("#numberOfTimesValueInputId").show('slow');
							jQuery("#numberOfTimesBetweenAndId").hide('slow');
							jQuery("#numberOfTimesBetweenEndId").hide('slow');
						}
						else if (selectedText.indexOf("Between") > -1){

							clearNTFields();
							
							jQuery("#numberOfTimesValueInputId").show('slow');
							jQuery("#numberOfTimesBetweenAndId").show('slow');							
							jQuery("#numberOfTimesBetweenEndId").show('slow');							
						}
					}
				);
			}
		);
	}
);

function clearNTFields()
{
	jQuery("#numberOfTimesValueStartInputTopTenId").val("");
	jQuery("#numberOfTimesBetweenEndInputId").val("");
	jQuery("#numberOfTimesValueStartInputId").val("");
	jQuery("#numberOfTimesBetweenEndInputTopTenId").val("");
}

jQuery(document).ready(
	function(){

		jQuery("#startDateInputId").datepicker({ 
			showOn: "both", 
			buttonImage: "./images/common/calendar.gif", 
			buttonImageOnly: true 
		}).attr("readonly", "readonly");

		jQuery("#endDateInputId").datepicker({ 
			showOn: "both", 
			buttonImage: "./images/common/calendar.gif", 
			buttonImageOnly: true 
		}).attr("readonly", "readonly");

	}
); 

function attachClickToPdLi()
{
	jQuery(".pdLi").click(
		function () {
			jQuery('#pdAjaxList ul').empty();
			jQuery("#personDepictedInputId").val(jQuery(this).text());
		}
	);
	
	jQuery(".pdLi").hover(
		function () {
   			jQuery(this).addClass("hiLitLi");
   		}, 
   		function () {
   			jQuery(this).removeClass("hiLitLi");
   		}
   	);
}

function attachClickToFtLi()
{
	jQuery(".ftLi").click(
		function () {
			var valStr = jQuery(this).text();
			
			var filmAndYear = valStr; // If there is no other film with the same name this var only holds the film title

			jQuery('#ftAjaxList ul').empty();
			jQuery("#filmTitleInputId").val(filmAndYear);
			jQuery("#filmTitleInputHiddenId").val(filmAndYear);

/*			
			var re =  /\(\d{4}\)/;
	
			if (re.test(filmAndYear))
			{
				var filmTitle = valStr.split(" (")[0];
				var filmYear = valStr.split(" (")[1].substring(0, 4);
	
				jQuery('#ftAjaxList ul').empty();
				jQuery("#filmTitleInputId").val(filmAndYear);
				jQuery("#filmTitleInputHiddenId").val(filmTitle);
				jQuery("#filmYearInputHiddenId").val(filmYear);
			}
			else
			{
				jQuery('#ftAjaxList ul').empty();
				jQuery("#filmTitleInputId").val(filmAndYear);
				jQuery("#filmTitleInputHiddenId").val(filmAndYear);
			}
*/
		}
	);
	
	jQuery(".ftLi").hover(
		function () {
   			jQuery(this).addClass("hiLitLi");
   		}, 
   		function () {
   			jQuery(this).removeClass("hiLitLi");
   		}
   	);
}

jQuery(document).ready(
	function(){
		jQuery("form").submit(
			function() {

				jQuery("select option:selected").each(
					function () {
						var selectedText = jQuery(this).text();
						if (selectedText.indexOf("Exactly") > -1){
							jQuery("#numberOfTimesBetweenEndInputId").val(jQuery("#numberOfTimesValueStartInputId").val());
						}
					}
				);
				
				jQuery("#startDateInputHiddenId").val(jQuery("#startDateInputId").val());
				jQuery("#endDateInputHiddenId").val(jQuery("#endDateInputId").val());
				jQuery("#numberOfTimesValueStartInputTopTenId").val(jQuery("#numberOfTimesValueStartInputId").val());
				jQuery("#numberOfTimesBetweenEndInputTopTenId").val(jQuery("#numberOfTimesBetweenEndInputId").val());

				return true;
			}
		);
	}
);
